/**
 * CommonService, includes user profile, file upload.
 */
// import { PrismaClient } from '@prisma/client';
import * as Koa from 'koa';
import * as Upload from '../utils/file';
import queryProfile from '../utils/user';

// const prisma = new PrismaClient();
export default class CommonService {
  hello = async () => {
    // TODO prisma demo.
    // const allUsers = await prisma.tb_profile_info.findMany();
    return new Promise(resolve => resolve('hello world'));
  };

  /**
   * General file upload
   * @returns file ids
   */
  upload = async (ctx: Koa.Context) => {
    const files = ctx.request.files;
    const origin = ctx.request.origin;
    const fileIds = await Upload.upload(files, origin);
  };

  /**
   * Add or update profile.
   * If no files, will update base info.
   * If params have id, denote update profile.Otherwise, add new profile.
   * @Return The new profileInfo.
   */
  saveAndFlushProfile = async (ctx: Koa.Context) => {
    // Get params
    const { id, userId, theme, motto } = ctx.request.body;
    const files = ctx.request.files;
    const profile: any = {
      userId: userId,
      theme: theme,
      motto: motto,
      // createTime: moment().format('YYYY-MM-DD HH:mm')
    };
    if (JSON.stringify(files) !== '{}') {
      const identifiers = await Upload.uploadAvatar(ctx);
      profile.avatarId = identifiers[0].id;
    }
  };

  /**
   * Delete user profile by id
   */
  deleteProfile = async (ctx: Koa.Context) => {
    const id = ctx.params.id;
  };
}
